package aa.db;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BookingServlet
 */
@WebServlet("/Booking")
public class BookingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");  
		String dishid = request.getParameter("dishid");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String amount = request.getParameter("amount");
        
        String restaurant_id = request.getParameter("restaurant_id");
       
        String[] tablenumbers = request.getParameterValues("tablenumber");
	   
	    for(String tablenumber : tablenumbers) {
	    String sql = "insert into shoppingcar(dishid,name,prise,amount,restaurant_id,order_status,tablenumber,all) values";
	    sql += "('"+dishid+"',"+name+",'+price+','"+amount+"','"+restaurant_id+"','预订',"+tablenumber+",";
	    sql +="datediff('"+amount+"')*(select price FROM tables ri LEFT JOIN dishes rt ON ri.restaurant_id=rt.restaurant_id WHERE tablenumber="+tablenumber+"))";
	    int ret = aa.db.DBTools.update(sql);
	    System.out.println("sql=" + sql);
	    System.out.println("ret=" + ret);
	    
	    }
	    //执行完成后返回页面
	    response.sendRedirect("shoppingcar.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
